/**
 * @file ui_toast.c
 * @author your name (you@domain.com)
 * @brief toast 
 * @version 0.1
 * @date 2023-06-10
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "ui_toast.h"


void toast_anim_deleted_cb(struct _lv_anim_t *a)
{
     lv_obj_t* msg_box = a->user_data;
     if(msg_box)
     {
        lv_obj_del(msg_box);
     }
        
}

void del_img_cb(lv_event_t *event)
{
    lv_obj_t *msgbox = (lv_obj_t *)lv_event_get_user_data(event);
    lv_obj_del(msgbox);
    COMLOG_INFO_LOG("del msgbox\n");
}

int ui_toast_show(char *text, TOAST_SHOW_TYPE_e type)
{
    if(text == NULL)
    {
        COMLOG_ERROR_LOG("text is null\n");
        return -1;
    }

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) lv_obj_set_y); 

    lv_disp_t *d_disp = lv_disp_get_default();
    int toast_ui_start_y = d_disp->driver->ver_res * TOP_UI_HEIGHT_RATE;   
    lv_obj_t* msg_box = lv_obj_create(lv_layer_top()); 
    lv_obj_clear_flag(msg_box, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_t* label_title = lv_label_create(msg_box);
    lv_obj_align(msg_box, LV_ALIGN_TOP_MID, 0,  toast_ui_start_y);
    lv_obj_set_size(msg_box , LV_PCT(100), d_disp->driver->ver_res * TOAST_UI_HEIGHT_RATE);
    
    switch(type)
    {
        case TOAST_SHOW_INFO:
        case TOAST_SHOW_INFO_WAIT_DEL:
          lv_obj_set_style_bg_color(msg_box, lv_color_hex(TOAST_UI_INFO_COLOR), 0); 
          break;
        case TOAST_SHOW_WARNING:
        case TOAST_SHOW_WARNING_WAIT_DEL:
          lv_obj_set_style_bg_color(msg_box, lv_color_hex(TOAST_UI_WARNING_COLOR), 0); 
          break;
        case TOAST_SHOW_ERROR:
        case TOAST_SHOW_ERROR_WAIT_DEL:
          lv_obj_set_style_bg_color(msg_box, lv_color_hex(TOAST_UI_ERROR_COLOR), 0); 
          break;
        default: break;
    }

    lv_obj_set_style_border_width(msg_box, 0, 0);
    lv_obj_set_style_radius(msg_box, 0, 0);
    lv_obj_set_style_text_color(label_title, lv_color_hex(TOAST_UI_TEXT_COLOR), 0);
    lv_obj_set_style_text_align(label_title, LV_TEXT_ALIGN_CENTER, 0);
   
       
    lv_label_set_long_mode(label_title, LV_LABEL_LONG_SCROLL);    
    lv_label_set_text(label_title, text);

    //if del, add del img
    if(type >= TOAST_SHOW_INFO_WAIT_DEL)
    {
        lv_obj_set_width(label_title, LV_PCT(80));
        lv_obj_align(label_title, LV_ALIGN_LEFT_MID, 0, 0); 
        lv_obj_t *img = lv_img_create(msg_box);
        lv_obj_align(img, LV_ALIGN_RIGHT_MID, 0, 0);
        lv_img_set_src(img,  LV_SYMBOL_CLOSE);
        lv_obj_add_flag(img,LV_OBJ_FLAG_CLICKABLE);
        lv_obj_add_event_cb(img, del_img_cb, LV_EVENT_CLICKED, msg_box);
    }
    else
    {
        lv_obj_set_width(label_title, LV_PCT(100));
        lv_obj_align(label_title, LV_ALIGN_CENTER, 0, 0); 
        lv_anim_set_var(&a, msg_box); 
        lv_anim_set_time(&a, TOAST_UI_SHOW_TIME);
        lv_anim_set_delay(&a, 1000);
        lv_anim_set_values(&a, toast_ui_start_y, 0);
        lv_anim_set_path_cb(&a, lv_anim_path_ease_in);
        lv_anim_set_deleted_cb(&a, toast_anim_deleted_cb);
        lv_anim_set_user_data(&a, (void *)msg_box);
        lv_anim_start(&a);
    }

    lv_obj_move_foreground(msg_box);
}